package com.gec.mall.service.impl;

import com.gec.mall.mapper.TbSpecificationMapper;
import com.gec.mall.mapper.TbSpecificationOptionMapper;
import com.gec.mall.pojo.PageResp;
import com.gec.mall.pojo.TbSpecification;
import com.gec.mall.pojo.TbSpecificationOption;
import com.gec.mall.pojo.TbSpecificationOptionExample;
import com.gec.mall.pojopro.Specification;
import com.gec.mall.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: li
 * @Date: 2023/1/8
 * @PACKAGE_NAME:com.gec.mall.service.impl
 * @Project_Name:mall-parent
 * @VERSION: 1.0.0
 */
@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private TbSpecificationMapper specificationMapper;

    @Autowired
    private TbSpecificationOptionMapper specificationOptionMapper;

    @Override
    public List<TbSpecification> findAll() {
        return specificationMapper.selectByExample(null);
    }

    @Override
    public PageResp findPage(int pageNum, int pageSize) {
        return null;
    }

    @Override
    public void add(Specification specification) {
        //保存规格一般的数据
        specificationMapper.insert(specification.getSpecification());
        Long id = specification.getSpecification().getId();
        //System.out.println("id=============="+id);
        //保存规格选项
        for (TbSpecificationOption specificationOption : specification.getSpecificationOptionList()) {
            //设置规格的id：  主键回填
            specificationOption.setSpecId(id);

            specificationOptionMapper.insert(specificationOption);
        }
    }

    @Override
    public void update(Specification specification) {
        //修改规格的一方
        specificationMapper.updateByPrimaryKey(specification.getSpecification());

        //先删除规格选项，再添加规格选项
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(specification.getSpecification().getId());
        specificationOptionMapper.deleteByExample(example);

        //保存规格选项
        for (TbSpecificationOption specificationOption : specification.getSpecificationOptionList()) {
            //设置规格的id：  主键回填
            specificationOption.setSpecId(specification.getSpecification().getId());

            specificationOptionMapper.insert(specificationOption);
        }
    }

    @Override
    public Specification findById(Long id) {
        Specification specification = new Specification();

        TbSpecification tbSpecification = specificationMapper.selectByPrimaryKey(id);

        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> tbSpecificationOptions = specificationOptionMapper.selectByExample(example);

        specification.setSpecification(tbSpecification);
        specification.setSpecificationOptionList(tbSpecificationOptions);
        return specification;
    }

    @Override
    public void delete(Long id) {
        specificationMapper.deleteByPrimaryKey(id);

        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        specificationOptionMapper.deleteByExample(example);
    }
}
